//
//  FQDSearchResultTableViewCell.m
//  FakeQDaily
//
//  Created by mewxiv on 9/30/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDSearchResultTableViewCell.h"
#import "NSDate+FQDExtension.h"
#import "FQDRestModel.h"

#import "Masonry.h"
#import "UIImageView+WebCache.h"

@interface FQDSearchResultTableViewCell ()

@property (nonatomic, strong) UIView *separator;
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *categoryLabel;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *lineLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subtitleLabel;

@end

@implementation FQDSearchResultTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configureCell];
    }
    return self;
}

- (void)configureCell
{
    _separator = [[UIView alloc] init];
    _separator.backgroundColor = RGB(233, 233, 233);
    [self.contentView addSubview:_separator];
    
    WeakSelf;
    [_separator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView);
        make.left.equalTo(weakSelf.contentView).offset(15);
        make.right.equalTo(weakSelf.contentView).offset(15);
        make.height.equalTo(@1);
    }];
    
    CGFloat iconSize = 20.0f;
    _iconImageView = [[UIImageView alloc] init];
    _iconImageView.layer.cornerRadius = iconSize / 2;
    [self.contentView addSubview:_iconImageView];
    
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.separator.mas_bottom).offset(13);
        make.left.equalTo(weakSelf.contentView).offset(15);
        make.size.equalTo(@(iconSize));
    }];
    
    _categoryLabel = [[UILabel alloc] init];
    _categoryLabel.font = FQDRegularFontWithSize(12.0f);
    _categoryLabel.text = @"aaa";
    [self.contentView addSubview:_categoryLabel];

    [_categoryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.iconImageView.mas_right).offset(9);
        make.centerY.equalTo(weakSelf.iconImageView);
        make.width.equalTo(@100);
    }];

    _nameLabel = [[UILabel alloc] init];
    _nameLabel.font = FQDRegularFontWithSize(12.0f);
    _nameLabel.textAlignment = NSTextAlignmentRight;
    _nameLabel.alpha = 0.5f;
    [self.contentView addSubview:_nameLabel];
    
    _lineLabel = [[UILabel alloc] init];
    _lineLabel.font = FQDRegularFontWithSize(12.0f);
    _lineLabel.textAlignment = NSTextAlignmentCenter;
    _lineLabel.alpha = 0.5f;
    _lineLabel.text = @"|";
    [self.contentView addSubview:_lineLabel];
    
    _dateLabel = [[UILabel alloc] init];
    _dateLabel.font = FQDRegularFontWithSize(12.0f);
    _dateLabel.textAlignment = NSTextAlignmentRight;
    _dateLabel.alpha = 0.5f;
    [self.contentView addSubview:_dateLabel];
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.lineLabel.mas_left).offset(-15);
        make.baseline.equalTo(weakSelf.categoryLabel);
    }];
    
    [_lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.dateLabel.mas_left).offset(-15);
        make.baseline.equalTo(weakSelf.categoryLabel);
    }];
    
    [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.contentView).offset(-15);
        make.baseline.equalTo(weakSelf.categoryLabel);
    }];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = FQDRegularFontWithSize(16.0f);
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_titleLabel];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.categoryLabel.mas_bottom).offset(13);
        make.left.equalTo(weakSelf.contentView).offset(17);
        make.right.equalTo(weakSelf.contentView).offset(-17);
    }];
    
    _subtitleLabel = [[UILabel alloc] init];
    _subtitleLabel.font = FQDRegularFontWithSize(12.0f);
    _subtitleLabel.numberOfLines = 2;
    [self.contentView addSubview:_subtitleLabel];
    
    [_subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.titleLabel.mas_bottom).offset(6);
        make.left.equalTo(weakSelf.titleLabel);
        make.right.equalTo(weakSelf.contentView).offset(-17);
        make.bottom.lessThanOrEqualTo(weakSelf.contentView.mas_bottomMargin);
    }];
}

- (void)setSearchResult:(FQDSearchResult *)searchResult
{
    FQDPost *post = searchResult.post;
    FQDAuthor *author = searchResult.author;
    
    NSURL *categoryImage = [NSURL URLWithString:post.category.normal];
    [self.iconImageView sd_setImageWithURL:categoryImage];
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:post.publish_time];
    NSString *dateString = [date dateStringSinceNow];
    
    self.categoryLabel.text = post.category.title;
    self.nameLabel.text = author.name;
    self.dateLabel.text = dateString;
    self.titleLabel.text = post.title;
    self.subtitleLabel.text = post.desc;
}

+ (NSString *)reuseId
{
    return @"FQDSearchResultTableViewCell";
}

@end
